<%@page contentType="text/html; charset=UTF-8" language="java" %>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html"  prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean"  prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@page import="com.kcs.common.utils.commonUtils"%>
<%@page import="com.kcs.common.utils.PropsUtils"%>
<%@page import="com.kcs.common.utils.Constant"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Setup Parameter</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript" src="jsp/script/common.js"></script>  
<link rel="stylesheet" type="text/css" href="jsp/css/table.css"/>
<link rel="stylesheet" type="text/css" href="jsp/css/fxmarginStyle.css"/>
<script type="text/javascript">

	var g_initialMargin;
	var g_maintenanceMargin;

	$(document).ready(function() {
		jQuery("#action").val("init");

		g_initialMargin 		= jQuery("#initialMargin").val();
		g_maintenanceMargin 	= jQuery("#maintenanceMargin").val();

		var message = jQuery("#message").val();

		if(message == "m_save_fail")	alert(m_save_fail);
		else if (message == "m_save_success") alert(m_save_success);

		jQuery("#message").val("");
	});

	function checkInputPercent(obj){

		if(isNumeric(obj.value)){
			if(obj.value > 99.99 ){
				obj.value = 99.99;
			}else if(obj.value < 0){
				obj.value = 0;
			}
		}else if(obj.value == null || obj.value == ""){
				
		}else {
			obj.value = 0;
		}

		calculateFxMarginLimit();
	}

	function isNumeric(n) {
		return !isNaN(parseFloat(n)) && isFinite(n);
	}

	function onSave(){
		var initialMargin 		= jQuery("#initialMargin").val();
		var maintenanceMargin 	= jQuery("#maintenanceMargin").val();

		if(initialMargin == null || initialMargin == "" || initialMargin == 0) {alert("Please fill required data in complete");}
		else if(maintenanceMargin == null || maintenanceMargin == "" || maintenanceMargin == 0) {alert("Please fill required data in complete");}
		else {

			//if(g_initialMargin == initialMargin && g_maintenanceMargin == maintenanceMargin) jQuery("#action").val("update");
			//else jQuery("#action").val("save");
			
			if(parseFloat(g_initialMargin) != parseFloat(initialMargin) || parseFloat(g_maintenanceMargin) != parseFloat(maintenanceMargin)) jQuery("#action").val("save");
			loading();
			submitform();
		}
	}

	function calculateFxMarginLimit(){
		var initialMargin 		= jQuery("#initialMargin").val();
		var fxMarginLimit		= 0;

		if(isNumeric(initialMargin)){			
			fxMarginLimit		= 100/initialMargin;
		}

		jQuery("#fxMarginLimit").val(fxMarginLimit.toFixed(2));
		
	}
	
</script>
</head>
<body>

<%@ include	file="include/headerMenu.jsp"%> 

<center>
	<html:form action="/SetupParameterAction.do">
	
		<html:hidden property="action" styleId="action"/>
		<html:hidden property="message" styleId="message"/>
	
		<div style="width:95%" align="center">
			<table width="100%" class="MarginHeader">
				<tr>
					<td align="left">
						Setup Parameter
					</td>
				</tr>
			</table>
			<div class="CriteriaBox">
				<table width="100%" class="MarginCriteria">
					<tr>
						<td width="50%" align="right"><font color="red">* </font>% of Initial Margin</td>
						<td width="50%" align="left" ><html:text property="initialMargin" onkeyup="checkInputPercent(this);" styleId="initialMargin" onchange="calculateFxMarginLimit();" maxlength="5"/> &nbsp;&nbsp; % of THB National Amount </td>
					</tr>
					<tr>
						<td width="50%" align="right"><font color="red">* </font>% of Maintenance Margin</td>
						<td width="50%" align="left" ><html:text property="maintenanceMargin" onkeyup="checkInputPercent(this);" styleId="maintenanceMargin" maxlength="5"/> &nbsp;&nbsp; % of Initial Margin </td>
					</tr>
					<tr>
						<td width="50%" align="right">FX Margin Limit(100/IM)</td>
						<td width="50%" align="left" ><html:text  property="fxMarginLimit" readonly="true" styleId="fxMarginLimit" styleClass="disabled"/> &nbsp;&nbsp; เท่าของ Excess Amount </td>
					</tr>
					<tr>
						<td width="100%" align="center" colspan="2">
							<input type="button" value="Save" class="button" onclick="onSave();" />
						</td>
					</tr>
				</table>
				
			</div>
		</div>
		<%@ include	file="include/footer.jsp"%> 
		<%@ include	file="include/message.jsp"%> 
	</html:form>	
</center>
</body>
</html>